package com.hqyj.ctxp.controller;

import com.hqyj.ctxp.pojo.Product;
import com.hqyj.ctxp.service.ProductService;
import com.hqyj.ctxp.utils.PageBean;
import com.hqyj.ctxp.utils.ResultInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 *  Product 对应的 Controller 类
 */
@RestController
@RequestMapping("/api/product")
public class ProductController {

    @Autowired
    private ProductService productService;

    // 添加商品
    @PostMapping("/productInfo")
    public ResultInfo addProduct(@RequestBody Product product) {
        ResultInfo resultInfo = productService.addProduct(product);
        return resultInfo;
    }

    // 127.0.0.1:8888/api/product/deleteProductByName/ -- delete
    // 根据商品名删除商品
    @DeleteMapping("/deleteProductByName/{productName}")
    public ResultInfo deleteProductByName(@PathVariable String productName) {
        ResultInfo resultInfo = productService.deleteProductByName(productName);
        return resultInfo;
    }

    // 根据商品种类，删除对应种类商品

    // 127.0.0.1:8888/api/product/selectProductPage -- post
    // 查询商品，分页查询、模糊查询
    @PostMapping("/selectProductPage")
    public ResultInfo selectProductPage(@RequestBody PageBean pageBean) {
        ResultInfo resultInfo = productService.selectProductPage(pageBean);
        return resultInfo;
    }

    // 127.0.0.1:8888/api/product/selectProductById -- post
    // 根据 productId 查询商品
    @GetMapping("/selectProductById/{productId}")
    public ResultInfo selectProductById(@PathVariable Integer productId) {
        ResultInfo resultInfo = productService.selectProductById(productId);
        return resultInfo;
    }

    // 127.0.0.1:8888/api/product/selectProductByCategoryId/ -- get
    // 根据种类 categoryId 查询商品
    @GetMapping("/selectProductByCategoryId/{categoryId}")
    public ResultInfo selectProductByCategoryId(@PathVariable Integer categoryId) {
        ResultInfo resultInfo = productService.selectProductByCategoryId(categoryId);
        return resultInfo;
    }

    // 修改商品信息
    // 127.0.0.1:8888/api/product/updateProductInfo -- put
    @PutMapping("/productInfo")
    public ResultInfo updateProductInfo(@RequestBody Product product) {
        ResultInfo resultInfo = productService.updateProductInfo(product);
        return resultInfo;
    }
}
